package com.atguigu04.compare;

public class Product implements Comparable{
    private String name;
    private double price;

    public Product() {
    }

    public Product(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
    /*
     *当前的类需要实现Comparable中的抽象方法: compareTo(0bject o)
     *在此方法中，指明如何判断当前类的对象的大小。比如:按照价格的高低进行大小的比较。(或从低到高排序)*
     **/

    @Override
    public int compareTo(Object o) {
        if(o==this)
        return 0;

        if(o instanceof Product){
            Product p=(Product) o;
            return Double.compare(p.price,this.price);
        }
        throw new RuntimeException("类型异常");

    }

    @Override
    public String toString() {
        return "Product{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}
